
#import "ClientController.h"


@interface ClientController ()

@property (readwrite, retain) NSNetServiceBrowser *browser;
@property (readwrite, retain) NSMutableArray *services;
@property (readwrite, assign) BOOL isConnected;
@property (readwrite, retain) NSNetService *connectedService;

@property (readwrite, assign) BOOL client;

@end

@implementation ClientController

@synthesize client;

@synthesize browser;
@synthesize services;
@synthesize isConnected;
@synthesize connectedService;

- (void)startClient
{
    // initialize array of services found
    self.services = [[NSMutableArray alloc] init];
    
    // running in autorelease mode
    //self.browser = [[NSNetServiceBrowser new] autorelease];
    
    // initialize new browser
    self.browser = [NSNetServiceBrowser new];
    
    // old method for assigning self delegation
    // self.browser.delegate = self;
    [browser setDelegate: self];
    self.isConnected = NO;
    
    // begin searching for server
    [self search];
}

-(void)dealloc {
    self.connectedService = nil;
    self.browser = nil;
    // running in autorelease mode
    // [services release];
    // [super dealloc];
}

-(void)search {
    NSLog(@"Hooray! Browser Search Started");
    [self.browser searchForServicesOfType:@"_bonjour_synergy_server._tcp." inDomain:@""];

    // inifinite loop
    client = true;
    while (client != false) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

- (void)stopClient
{
    NSLog(@"Hooray! Synergy Stopped");
    [synergy2 interrupt];
    // [synergy2 release];
    synergy2 = nil;
}

#pragma mark Net Service Browser Delegate Methods
-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didFindService:(NSNetService *)aService moreComing:(BOOL)more {
    NSLog(@"Hooray! Bonjour Service Found");
    
    // client != false
    // add object to end of array
    [self.services addObject:aService];
    NSLog(@"Service Found: %@", aService);
    
    // remoteService.delegate = self;
    // NSLog(@"%@", [NSString stringWithFormat:@"Resolved:%ld\n", [aService port]]);
    [aService setDelegate: self];
    [aService resolveWithTimeout:0];
}

-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didRemoveService:(NSNetService *)aService moreComing:(BOOL)more {
    NSLog(@"Hooray! Bonjour Service Removed");
    // [servicesController removeObject:aService];
    if ( aService == self.connectedService ) self.isConnected = NO;
    
    // #TODO need to stop client
    [self stopClient];
    
}

-(void)netServiceDidResolveAddress:(NSNetService *)service {
    NSLog(@"Hooray! Bonjour Service Resolved");
    self.isConnected = YES;
    self.connectedService = service;
    NSArray *args;
    NSString *binaryPath;
    
    // start client
    binaryPath = @"~/.bonjour-synergy/synergyc";
    NSLog(@"path: %@",binaryPath);
    synergy2 = [[NSTask alloc] init];
    [synergy2  setLaunchPath:binaryPath];
    args = [NSArray arrayWithObjects:@"-f", [service hostName], nil];
    [synergy2 setArguments:args];
    [synergy2 launch];
    NSLog(@"Hooray! Client did launch");

    
}

-(void)netService:(NSNetService *)service didNotResolve:(NSDictionary *)errorDict {
    NSLog(@"Hooray! Could not resolve: %@", errorDict);
}

@end
